// 1.入口函数
// 2.根组件
// 3.页面加载

import 'package:flutter/material.dart';

void main() {
  // ignore: prefer_const_constructors
  runApp(MaterialApp(
    // 无状态组件
    // home: Scaffold(
    //   appBar: AppBar(
    //     title: const Text('页面架子'),
    //     backgroundColor: Colors.red,
    //   ),
    //   body: MyInfo(
    //     name: 'Jack',
    //     age: 27,
    //   ),
    // ),

    // 有状态组件
    home: const MyCounter(),
  ));
}

// 1.无状态组件
// ignore: must_be_immutable
class MyInfo extends StatelessWidget {
  // 构造函数
  MyInfo({super.key, required this.name, required this.age});
  // 传参
  String name;
  int age;
  // build函数
  @override
  Widget build(BuildContext context) {
    return Center(
      // child: Text('Who Am I?'),
      child: Text(
        '$name今年$age岁',
        style: const TextStyle(color: Colors.red, fontSize: 20.0),
      ),
    );
  }
}

// 2.有状态组件
class MyCounter extends StatefulWidget {
  // ignore: use_super_parameters
  const MyCounter({Key? key}) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _MyCounterState createState() => _MyCounterState();
}

class _MyCounterState extends State<MyCounter> {
  // 状态
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("计算器组件"),
        backgroundColor: Colors.lightBlue,
      ),
      body: Center(
          child: Text(
        count.toString(),
        style: const TextStyle(color: Colors.red, fontSize: 40),
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 更改状态 setState
          setState(() {
            count++;
          });
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}
